# 写入文件和读取文件写法比较类似
# 写入如的时候我们用 w 模式, 用 write() 方法写入文件内容
# w 代表每次写入时, 会先清空文件内容, 再写入新的内容,如果文件不存在,则会创建文件
from common_functions import cut_block as cb
cb("写入文件")
with open('content/pi_write.txt', 'w') as t:
    t.write("3.1415926")


cb("写入文件后读取文件")
with open('content/pi_write.txt', 'w+') as t:
    t.write("3.1415926")
    t.seek(0)  # 将文件指针移回文件开头
    content = t.read()
    print("写入后读取的内容:", content)

cb("追加写入文件后读取文件")
with open('content/pi_write.txt', 'a+') as t:
    t.write("8979323846")
    t.seek(0)  # 将文件指针移回文件开头
    content = t.read()
    print("读取后的内容:", content)
